package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.scene.service.QuerySceneInstService;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 大数据上传附件限制一个
 *  * 每次上传覆盖旧值
 */
@Service
public class BigDataUploadLimitAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private QuerySceneInstService querySceneInstService;
    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        String serviceOfferId = data.getServiceOfferId();
        if(KiteStringUtils.isNotEmpty(value)){
            JSONArray jsonArray = JSON.parseArray(value);
            //判断此时除删除外有效附件的个数 大于1的时候处理覆盖逻辑
            long count = jsonArray.stream().filter(jsonAttr -> {
                JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(jsonAttr));
                return !KiteStringUtils.equals(jsonObject.getString("actionType"), ActionType.D.name());
            }).count();
            if(count>1){
                JSONArray newJsonArry = new JSONArray();
                Iterator<Object> iterator = jsonArray.iterator();
                boolean isDel = false;//是否已处理覆盖
                while (iterator.hasNext()){
                    Object attrObj = iterator.next();
                    JSONObject attrJson = JSONObject.parseObject(JSON.toJSONString(attrObj));
                    //如果已处理过覆盖直接添加到最后结果
                    if(isDel){
                        newJsonArry.add(attrJson);
                        continue;
                    }
                    String thisActionType = attrJson.getString("actionType");
                    if(KiteStringUtils.equals(thisActionType, ActionType.D.name())){
                        newJsonArry.add(attrJson);
                        continue;
                    }
                    //如果是第一个有效附件是新增 则不添加到最终结果 标记已覆盖
                    if(KiteStringUtils.equals(thisActionType, ActionType.A.name())){
                        isDel = true;
                        continue;
                    }
                    //如果是第一个有效附件有原始值 则处理为标记添加到最后结果 标记已覆盖
                    if(KiteStringUtils.isNotEmpty(attrJson.getString("originValue"))&&!KiteStringUtils.equalsAny(serviceOfferId, ServiceOfferConsts.SERVICE_OFFER_SJZD,ServiceOfferConsts.SERVICE_OFFER_ORDER)){
                        attrJson.put("actionType",KeyConsts.PROD_ATTR_SHOW_TYPE_D);
                        newJsonArry.add(attrJson);
                        isDel = true;
                        continue;
                    }
                    newJsonArry.add(attrJson);
                }
                value = newJsonArry.toJSONString();
            }
        }
        super.set(attr, inst, value, data);
    }
}
